<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="login-nav-bar" title="登录" >
      <van-icon name="cross" slot="left" @click="$router.back()" />
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxLength="11"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        v-model="user.code"
        name="验证码"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxLength="6"
      >
        <!-- icon插槽 -->
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <!-- button插槽 -->
        <template #button>
          <!-- 倒计时组件countDown -->
          <van-count-down
            :time="1000 * 60"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <!-- 验证码button -->
          <van-button
            v-else
            class="btn-send-sms"
            round
            size="small"
            type="default"
            @click="onSendSms"
            native-type="button"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login,sendSms } from "@/api/user";
import {mapMutations} from 'vuex'
export default {
  name: "LoginIndex",
  data() {
    return {
      user: {
        mobile: "13886883211",
        code: "246810",
      },
      userFormRules: {
        mobile: [
          { required: true, message: "请填写手机号" },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: "手机号格式错误" },
        ],
        code: [
          { required: true, message: "请填写验证码" },
          { pattern: /^\d{6}$/, message: "验证码格式错误" },
        ],
      },
      isCountDownShow: false,
    };
  },
  methods: {
    ...mapMutations(['setUser']),
    async onSubmit() {
      // console.log("ok");
      // 1.接收数据
      const user = this.user;

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      // 2.发送ajax
      try {
        const {data} = await login(user);
       
        this.$toast.success("登录成功");
        this.setUser(data.data)
        this.$router.back()
        // console.log(res);
      } catch (err) {
        // console.log(err);
        this.$toast.fail("登录失败");
        // console.log(err);
      }
    },
    async onSendSms() {
      // console.log("ok");
      // 1.验证手机号是否合法
      try {
        await this.$refs.loginFormRef.validate("mobile");
      } catch (err) {
        return 
      }

      // 2.展示倒计时效果
      this.isCountDownShow = true;
      // 3.发送ajax获取验证码
      try {
        await sendSms(this.user.mobile)
      }catch(err){
        // console.log(err);
        this.isCountDownShow = false
        return  this.$toast.fail("获取验证码失败")
      }
      

    },
  },
  created() {},
};
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .btn-send-sms {
    width: 152px;
    height: 46px;
    line-height: 46px;
    padding: 0;
    background-color: #ececec;
    .van-button__text {
      color: #858585;
    }
  }
  .login-btn-wrap {
    margin: 51px 30px 0;
    .van-button {
      background-color: #6db4fa;
      border: none;
      .van-button__text {
        font-size: 30px;
      }
    }
  }
}
</style>